package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
@Slf4j
@RestController
@RequestMapping("/admin/report")
public class ReportController {
    @Autowired
    private ReportService reportService;

    @GetMapping("/turnoverStatistics")
    public Result report(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ) {
        try {
            TurnoverReportVO report = reportService.report(begin, end);
            if (report != null) {
                return Result.success(report);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.error("未知错误");
    }
    @GetMapping("ordersStatistics")
    public Result validorder(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    )
    {
        try {
            OrderReportVO orderReportVO = reportService.vaildorder(begin, end);
            if (orderReportVO != null) {
                return Result.success(orderReportVO);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.error("未知错误");
    }
    @GetMapping("/top10")
    public Result top10(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    )
    {
        try {
            SalesTop10ReportVO salesTop10Report = reportService.top10(begin, end);
            log.info("salesTop10Report is {}", salesTop10Report);
            if (salesTop10Report != null) {
                return Result.success(salesTop10Report);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return Result.error("未知错误");
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response)
    {
        reportService.export(response);
    }
}
